//
//  AsynConnect.m
//  MMLOOK
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "AsynConnect.h"
#import "Reachability.h"

@interface AsynConnect ()<NSURLConnectionDataDelegate, NSURLConnectionDelegate>
@property (nonatomic, retain)NSString *str;

@end

@implementation AsynConnect

- (void)dealloc
{
    [_receiveData release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.receiveData  = [NSMutableData data];
    }
    return self;
}

+ (BOOL)judgeConnect
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    NetworkStatus states = [reach currentReachabilityStatus];
    if (states == NotReachable) {
        NSLog(@"此刻无网络连接");
        return NO;
    } else if (states == ReachableViaWiFi)
    {
        return YES;
    } else if (states == ReachableViaWWAN)
    {
        return YES;
    } else
    {
        return NO;
    }
    
}


+ (instancetype)asynConnectWithUrlstr: (NSString *)Urlstr parmater: (NSMutableDictionary *)parmater block: (void(^)(NSData *data))block
{
    NSString *s = @"";
    
    for (NSString *key in parmater) {
        if ([s length] == 0) {
            s = [NSString stringWithFormat:@"?%@=%@", key, [parmater objectForKey:key]];
        }
        s = [NSString stringWithFormat:@"%@&%@=%@", s, key, [parmater objectForKey:key]];
    }
    
    Urlstr = [NSString stringWithFormat:@"%@%@", Urlstr, s];
    //Urlstr = [Urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    BOOL judge = [AsynConnect judgeConnect];
    if (judge) {
        AsynConnect *as = [[[AsynConnect alloc] init] autorelease];
        as.str = [NSString stringWithFormat:@"%@", Urlstr];
        [as startConnectWithUrl:Urlstr];
        as.finish = block;
        return as;
    } else {
        //将链接保存
        NSString *path = Urlstr;
        NSLog(@"pathppppp = %@", path);
        path = [path stringByReplacingOccurrencesOfString:@"/" withString:@""];
        path = [path stringByReplacingOccurrencesOfString:@":" withString:@""];
        NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *file = [NSString stringWithFormat:@"%@/%@",[array firstObject],path];
        
        NSFileManager *manger = [NSFileManager defaultManager];
        
        if ([manger fileExistsAtPath:file]) {
            
            NSMutableData *data = [NSMutableData dataWithContentsOfFile:file];
            AsynConnect *as = [[[AsynConnect alloc] init] autorelease];
            as.finish = block;
            as.finish(data);
        } else {
            
            NSLog(@"无此链接  空");
        }
    }
    
    return nil;
}


- (void)startConnectWithUrl: (NSString *)Url
{
    NSURL *url = [NSURL URLWithString:Url];
    
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
    
    [request setHTTPMethod:@"GET"];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@" 收到响应");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.finish(self.receiveData);
    //获取连接
//    NSString *path = self.str;
//    path = [path stringByReplacingOccurrencesOfString:@"/" withString:@""];
//    path = [path stringByReplacingOccurrencesOfString:@":" withString:@""];
//    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
//    NSString *file = [NSString stringWithFormat:@"%@/%@", [array firstObject],path];
//    [self.receiveData writeToFile:file atomically:YES];
//    NSLog(@"缓存地址 = %@", path);
}

@end
